import { Component, OnInit, ViewChild, OnDestroy, AfterContentInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, UploadFile, NzModalService, NzNotificationService } from 'ng-zorro-antd';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import {SettingsService, TitleService} from '@delon/theme';
import { Constant, ServiceResult } from '@core/constant';
import { noticeService } from '../../../../service/notice/notice.service';
import { EmitterService } from 'app/service/emit.service';
import { CommService } from '../../../../service/comm/comm.service';
import {SimpleTableColumn, SimpleTableComponent} from "@delon/abc";

@Component({
  selector: 'notice-view-edit',
  templateUrl: './notice-edit.component.html',
  styleUrls: ['./notice-edit.component.less']
})
export class noticeViewEditComponent implements OnInit, OnDestroy {
  product: any = {
    bpmStatus: null,
    id: null,
    ntBegindate:null,
    ntContent: null,
    ntFreq: null,
    ntHead: null,
    ntMobile: null,
    ntName: null,
    ntNo: null,
    ntObjId: null,
    ntOpenid: null,
    ntStatus: null,
    ntType: null,
    ntUser: null,
    orderNo: null,
    fxjOut01: null,fxjOut02: null,
    fxjOut03: null,fxjOut04: null,
    fxjOut05: null,fxjOut06: null,
    fxjOut07: null,fxjOut08: null,
    fxjOut09: null,fxjOut10: null,
    fxjOut11: null,fxjOut12: null,
    fxjOut13: null,fxjOut14: null,
    fxjOut15: null,fxjOut16: null,
    fxjOut17: null,fxjOut18: null,
    fxjOut19: null,fxjOut20: null,
    fxjOut21: null,fxjOut22: null,
    fxjOut23: null,fxjOut24: null,
    fxjOut25: null,fxjOut26: null,
    fxjOut27: null,fxjOut28: null,
    fxjOut29: null,fxjOut30: null,
    fxjOut31: null,fxjOut32: null,
    fxjOut33: null,fxjOut34: null,
    fxjOut35: null,fxjOut36: null,
    fxjOut37: null,fxjOut38: null,
    fxjOut39: null,fxjOut40: null,
    fxjOut41: null,fxjOut42: null,
    fxjOut43: null,fxjOut44: null,
    fxjOut45: null,fxjOut46: null,
    fxjOut47: null,fxjOut48: null,
    fxjOut49: null,fxjOut50: null,
    fxjOut51: null,fxjOut52: null,
    fxjOut53: null,fxjOut54: null,
    fxjOut55: null,fxjOut56: null,
    fxjOut57: null,fxjOut58: null,
    fxjOut59: null,fxjOut60: null,
    fxjOut61: null,fxjOut62: null,
    fxjOut63: null,fxjOut64: null,
    fxjOut65: null,fxjOut66: null,
    fxjOut67: null,fxjOut68: null,
    fxjOut69: null,fxjOut70: null,
    fxjOut71: null,fxjOut72: null,
    fxjOut73: null,fxjOut74: null,
    fxjOut75: null,fxjOut76: null,
    fxjOut77: null,fxjOut78: null,
    fxjOut79: null,fxjOut80: null

  };

  productorder: any = {
    id:null,
    bmDate: null,
    bmTime: null,
    bxRq: null,
    carColor: null,
    carDetail: null,
    carVin: null,
    conJson: null,
    conNo: null,
    conType: null,
    conUrl: null,
    createSysorg: null,
    createUser: null,
    cusAge: null,
    cusBirthday: null,
    cusDeu: null,
    cusEthnical: null,
    cusGender: null,
    cusIdValidfrom: null,
    cusIdValidto: null,
    cusIncard: null,
    cusMaritalStatus: null,
    cusMobile: null,
    cusName: null,
    cusQqid: null,
    cusRemark: null,
    cusResAddr: null,
    cusWechatid: null,
    cusrIdtype: null,
    danganNo: null,
    diyaStatus: null,
    diyaUser: null,
    fangkBank: null,
    fangkId: null,
    fangkName: null,
    fangkType: null,
    fkAmount: null,
    gpsInsAddr: null,
    gpsInsData: null,
    gpsInsMobile: null,
    gpsInsUser: null,
    gpsStatus: null,
    gpsUser: null,
    guohuStatus: null,
    guohuUser: null,
    luqiaoRq: null,
    lxMobile1: null,
    lxMobile2: null,
    lxMobile3: null,
    lxName1: null,
    lxName2: null,
    lxName3: null,
    mpName: null,
    mpUrl: null,
    mpbackUrl: null,
    nianjiaRq: null,
    orderAmount: null,
    orderArea: null,
    orderBy1: null,
    orderBy10: null,
    orderBy11: null,
    orderBy12: null,
    orderBy13: null,
    orderBy14: null,
    orderBy15: null,
    orderBy16: null,
    orderBy17: null,
    orderBy18: null,
    orderBy2: null,
    orderBy3: null,
    orderBy4: null,
    orderBy5: null,
    orderBy6: null,
    orderBy7: null,
    orderBy8: null,
    orderBy9: null,
    orderBy27:null,

    orderBy28:null,

    orderBy29:null,

    orderBy30:null,
    orderCarno: null,
    orderCartype: null,
    orderInfo: null,
    orderNo: null,
    orderOper: null,
    orderPeriod: null,
    orderStatus: null,
    proClass: null,
    proIsdb: null,
    proName: null,
    proNo: null,
    proType: null,
    proWf: null,
    qiandanUser: null,
    sfAnpaijiafang: null,
    sfDianshen: null,
    wbName: null,
    wbUrl: null,
    wbbackUrl: null,
    yinhangUser: null,
    dypname: null,
    dypvalue: null,
    dypyear: null,
    dyptype: null,
    dypclass: null,

    dypremark1: null,
    dypremark2: null,
    dypremark3: null,
    emergencyRelation:  null,
    emergencyContact: null,
    emergencyMobile: null,
    emergencyOffice:null,
    emergencyResAddr:null,
    spcomment:'同意',
    bt1:null,
    bt2:null,
    bt3:null,
  }

  productfk: any = {
    bpmStatus: null,
    bxFy: null,
    bxRq: null,
    carChengshu: null,
    carPinggujia: null,
    carRongzie: null,
    conJine: null,
    createBy: null,
    createDate: null,
    createName: null,
    cusId: null,
    cusMobile: null,
    cusName: null,
    daoshouJine: null,
    fkBy1: null,
    fkBy10: null,
    fkBy11: null,
    fkBy12: null,
    fkBy13: null,
    fkBy2: null,
    fkBy3: null,
    fkBy4: null,
    fkBy5: null,
    fkBy6: null,
    fkBy7: null,
    fkBy8: null,
    fkBy9: null,
    gpsFy: null,
    id: null,
    liulFy: null,
    loanAmount: null,
    loanAtta: null,
    loanDate: null,
    loanDbmonth: null,
    loanMonth: null,
    loanMonthSum: null,
    loanPeriod: null,
    loanReMethod: '',
    loanRemark: null,
    loanSbamount: null,
    loanSum: null,
    luqiaoRq: null,
    lydbFuwfei: null,
    nianjianRq: null,
    orderNo: null,
    qitaFy: null,
    sysCompanyCode: null,
    sysOrgCode: null,
    updateBy: null,
    updateDate: null,
    updateName: null,
    ygJine: null,
  }

  productpl:  any = {
    id:null,
    orderNo: null,
    bpmStatus:null,
    orderInfo: null,
    cusName: null,
    refPeriod: null,
    refTotalPeriod: null /*No show*/,
    refDate: null,
    refFbenjin: null,
    refAmount: null,
    refLixi: null,
    refSybenjin: null,
    reMark: null,
    payAmount: null
  };

  ps = 100;

  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 紧急联系人
  emen:  any = {
    emergencyContact:null,
    emergencyMobile: null,
    emergencyOffice:null
  };
  columnsemen: SimpleTableColumn[] = [
    { title: '姓名 ', index: 'emergencyContact' },
    { title: '电话 ', index: 'emergencyMobile' },
    { title: '身份证', index: 'emergencyOffice' }
  ];

  emenurl="";
  url=this.noticeService.dhlist();

  columnsyg: SimpleTableColumn[] = [
    { title: '应还款日期 ', index: 'fxjOut01'},
    { title: '月供金额', index: 'fxjOut03'},
    { title: '逾期天数', index: 'fxjOut04'},
    { title: '逾期违约金', index: 'fxjOut05'},
    { title: '违约金处理状态', index: 'fxjOut06'},
    { title: '收款次数', index: 'fxjOut07'},
    { title: '还款类型', index: 'fxjOut08'},
    { title: '收款方式', index: 'fxjOut09'},
    { title: '月供收款金额', index: 'fxjOut10'},
    { title: '还款日期', index: 'fxjOut11'},
    { title: '催收人', index: 'fxjOut13'},
    { title: '备注', index: 'fxjOut58'},
  ];
  columnsnj: SimpleTableColumn[] = [
    { title: '年检有效期止 ', index: 'fxjOut16'},
    { title: '应收金额', index: 'fxjOut17'},
    { title: '新年检有效止', index: 'fxjOut18'},
    { title: '收款次数', index: 'fxjOut19'},
    { title: '还款类型', index: 'fxjOut20'},
    { title: '收款方式', index: 'fxjOut21'},
    { title: '收款金额', index: 'fxjOut22'},
    { title: '还款日期', index: 'fxjOut23'},
    { title: '催收人', index: 'fxjOut25'},
    { title: '备注', index: 'fxjOut59'},
  ];
  columnssybx: SimpleTableColumn[] = [
    { title: '商业保险金额 ', index: 'fxjOut28'},
    { title: '投保公司', index: 'fxjOut29'},
    { title: '商业保险到期日期', index: 'fxjOut30'},
    { title: '商业续保报价金额', index: 'fxjOut31'},
    { title: '收款次数', index: 'fxjOut34'},
    { title: '还款类型', index: 'fxjOut35'},
    { title: '收款方式', index: 'fxjOut36'},
    { title: '收款金额', index: 'fxjOut37'},
    { title: '还款日期', index: 'fxjOut38'},
    { title: '催收人', index: 'fxjOut40'},
    { title: '备注', index: 'fxjOut60'},
  ];
  columnsjqbx: SimpleTableColumn[] = [
    { title: '交强保险金额 ', index: 'fxjOut43'},
    { title: '投保公司', index: 'fxjOut44'},
    { title: '交强保险到期日期', index: 'fxjOut45'},
    { title: '交强续保报价金额', index: 'fxjOufxjOut46t31'},
    { title: '收款次数', index: 'fxjOut49'},
    { title: '还款类型', index: 'fxjOut50'},
    { title: '收款方式', index: 'fxjOut51'},
    { title: '收款金额', index: 'fxjOut52'},
    { title: '还款日期', index: 'fxjOut53'},
    { title: '催收人', index: 'fxjOut55'},
    { title: '备注', index: 'fxjOut61'},
  ];

  bpmStatus =  [{text: '未放款', value: '1'}, {text: '已放款', value: '10'}];
  loanReMethods =  [{text: '等额本金', value: '10'}, {text: '等额本息', value: '20'}, {text: '先息后本', value: '30'}];
  MAX_COUNT = 60;
  tabIndex = 0;
  saleing = false;

  noticeTy = 0;

  uploadUr5 = '';
  avatarParam5 = { sbtype: 'No',  filetype:'jj',orderNo: 'no'};
  fileList = [ ];
  loading = false;
  // 预览图片对象
  previewImage = '';
  // 是否可以预览
  previewVisible = false;

  private sub: any;

  constructor(
    public setting: SettingsService,
    private msg: NzMessageService,
    private modalService: NzModalService,
    private noticeService: noticeService,
    private emit: EmitterService,
    private notification: NzNotificationService,
    private route: ActivatedRoute,
    private titleService: TitleService,
    private cnt: Constant,
    private router: Router,
    private confirmServ: NzModalService,
    private comm: CommService
  ) {}

  ngOnInit(): void {
    // this.loadbpmStatus();
    // 获取参数
    this.sub = this.route.params.subscribe(params => {
      this.product.id = params["id"];
      console.log("this.product.id****************"+this.product.id);
      // 获取商品详情
      this.getProductInfo();
    });
    // 隐藏reusetab
    setTimeout(() => {
        this.emit.sendMessage("HIDE_REUSE_TAB");
    }, 100);
    this.titleService.setTitle("编辑产品");

    this.uploadUr5 = this.noticeService.uploadUrl();

  }
  // 加载证件类型
  loadbpmStatus() {
    this.bpmStatus = [];
    this.comm.getType("bpm_status").subscribe(data => {
      const ret: ServiceResult = <ServiceResult> data;
      if (ret.ok ) {
        for (const row of ret['data']) {
          this.bpmStatus.push({ text: row.typename, value: row.typecode });
        }
      }
    })
  }
  // 显示reusetab
  ngOnDestroy() {
    this.emit.sendMessage("SHOW_REUSE_TAB");
  }

  changeTab(index) {
    // 使用异步的方式更新组件, 防止ngIf错误
    Promise.resolve(null).then(() => this.tabIndex = index);
  }



  getProductInfo() {
    this.noticeService.getProduct(this.product.id).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.product = data.data;
      this.product.loanDate="";
      this.avatarParam5 = { sbtype: 'No',  filetype:'dh',orderNo: this.product.orderNo};
      this.product.ntStatus="已处理";
      this.getOrder(); // 订单信息
      this.getProductInfofk(); // 放款
      this.getProductInfopl(); // 还款
      this.emenurl = this.noticeService.emenurl()+this.product.orderNo;
      console.log(this.product);
      this.noticeTy = this.product.ntType
      console.log(this.noticeTy);
      if(this.product.ntType == '商业保险'){
        this.noticeTy = 1;
      }
      if(this.product.ntType == '交强保险'){
        this.noticeTy = 2;
      }
      if(this.product.ntType == '违章'){
        this.noticeTy = 5;
      }
      if(this.product.ntType == '月供'){
        this.noticeTy = 4;
      }
      if(this.product.ntType == '年检'){
        this.noticeTy = 5;
      }
    })
  }

  getOrder(){
    this.noticeService.getCusInfo(this.product.orderNo).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        if(data.ok){
          this.productorder = data.data;
          console.log(this.productorder);
          if (data.data.fxjOrderAttaList.length > 0) {
            let flag = 0;
            for (const detail of data.data.fxjOrderAttaList) {
              this.fileList.push( {
                uid: flag,
                name: detail.attaName,
                status: 'done',
                url: detail.attaUrl,
                id: detail.id
              } )
              flag++;
            }
          };
        }else{
          console.log("22")
        }
      })
  }

  getProductInfofk() {
    this.noticeService.getProductFk(this.product.orderNo).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if(data.ok){
        this.productfk = data.data;
        console.log(this.productfk);
      }else{
        // this.productfk.loanReMethod='10';
      }

      // this.productfk.loanDate="";
    })
  }
  getProductInfopl() {
    this.noticeService.getProductPl(this.product.orderNo).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if(data.ok){
        this.productpl = data.data;
        console.log(this.productfk);
      }else{
        // this.productfk.loanReMethod='10';
      }

      // this.productfk.loanDate="";
    })
  }



  back() {
    this.router.navigate(['/notice/notice']);
  }
  backPl() {
    this.router.navigate(['/pl/replan']);
  }
  reload() {
    this.router.navigate(['/notice/notice/edit/'+this.product.id]);
  }


  save() {

    let fxjLoanNotice = {
        bpmStatus: this.product.bpmStatus,
        createBy: this.product.createBy,
        createDate: this.product.createDate,
        createName: this.product.createName,
        fxjOut01: this.product.fxjOut01,
        fxjOut02: this.product.fxjOut02,
        fxjOut03: this.product.fxjOut03,
        fxjOut04: this.product.fxjOut04,
        fxjOut05: this.product.fxjOut05,
        fxjOut06: this.product.fxjOut06,
        fxjOut07: this.product.fxjOut07,
        fxjOut08: this.product.fxjOut08,
        fxjOut09: this.product.fxjOut09,
        fxjOut10: this.product.fxjOut10,
        fxjOut11: this.product.fxjOut11,
        fxjOut12: this.product.fxjOut12,
        fxjOut13: this.product.fxjOut13,
        fxjOut14: this.product.fxjOut14,
        fxjOut15: this.product.fxjOut15,
        fxjOut16: this.product.fxjOut16,
        fxjOut17: this.product.fxjOut17,
        fxjOut18: this.product.fxjOut18,
        fxjOut19: this.product.fxjOut19,
        fxjOut20: this.product.fxjOut20,
        fxjOut21: this.product.fxjOut21,
        fxjOut22: this.product.fxjOut22,
        fxjOut23: this.product.fxjOut23,
        fxjOut24: this.product.fxjOut24,
        fxjOut25: this.product.fxjOut25,
        fxjOut26: this.product.fxjOut26,
        fxjOut27: this.product.fxjOut27,
        fxjOut28: this.product.fxjOut28,
        fxjOut29: this.product.fxjOut29,
        fxjOut30: this.product.fxjOut30,
        fxjOut31: this.product.fxjOut31,
        fxjOut32: this.product.fxjOut32,
        fxjOut33: this.product.fxjOut33,
        fxjOut34: this.product.fxjOut34,
        fxjOut35: this.product.fxjOut35,
        fxjOut36: this.product.fxjOut36,
        fxjOut37: this.product.fxjOut37,
        fxjOut38: this.product.fxjOut38,
        fxjOut39: this.product.fxjOut39,
        fxjOut40: this.product.fxjOut40,
        fxjOut41: this.product.fxjOut41,
        fxjOut42: this.product.fxjOut42,
        fxjOut43: this.product.fxjOut43,
        fxjOut44: this.product.fxjOut44,
        fxjOut45: this.product.fxjOut45,
        fxjOut46: this.product.fxjOut46,
        fxjOut47: this.product.fxjOut47,
        fxjOut48: this.product.fxjOut48,
        fxjOut49: this.product.fxjOut49,
        fxjOut50: this.product.fxjOut50,
        fxjOut51: this.product.fxjOut51,
        fxjOut52: this.product.fxjOut52,
        fxjOut53: this.product.fxjOut53,
        fxjOut54: this.product.fxjOut54,
        fxjOut55: this.product.fxjOut55,
        fxjOut56: this.product.fxjOut56,
        fxjOut57: this.product.fxjOut57,
        fxjOut58: this.product.fxjOut58,
        fxjOut59: this.product.fxjOut59,
        fxjOut60: this.product.fxjOut60,
        fxjOut61: this.product.fxjOut61,
        fxjOut62: this.product.fxjOut62,
        fxjOut63: this.product.fxjOut63,
        fxjOut64: this.product.fxjOut64,
        fxjOut65: this.product.fxjOut65,
        fxjOut66: this.product.fxjOut66,
        fxjOut67: this.product.fxjOut67,
        fxjOut68: this.product.fxjOut68,
        fxjOut69: this.product.fxjOut69,
        fxjOut70: this.product.fxjOut70,
        fxjOut71: this.product.fxjOut71,
        fxjOut72: this.product.fxjOut72,
        fxjOut73: this.product.fxjOut73,
        fxjOut74: this.product.fxjOut74,
        fxjOut75: this.product.fxjOut75,
        fxjOut76: this.product.fxjOut76,
        fxjOut77: this.product.fxjOut77,
        fxjOut78: this.product.fxjOut78,
        fxjOut79: this.product.fxjOut79,
        fxjOut80: this.product.fxjOut80,
        sysCompanyCode: this.product.sysCompanyCode,
        sysOrgCode: this.product.sysOrgCode,
        updateBy: this.product.updateBy,
        updateDate: this.product.updateDate,
        updateName: this.product.updateName
    };
    let fxjLoanNoticea = {
      bpmStatus: this.product.bpmStatus,
      createBy: this.product.createBy,
      createDate: this.product.createDate,
      createName: this.product.createName,
      id: this.product.id,
      ntBegindate: this.product.ntBegindate,
      ntContent: this.product.ntContent,
      ntFreq: this.product.ntFreq,
      ntHead: this.product.ntHead,
      ntMobile: this.product.ntMobile,
      ntName: this.product.ntName,
      ntNo: this.product.ntNo,
      ntObjId: this.product.ntObjId,
      ntOpenid: this.product.ntOpenid,
      ntStatus: this.product.ntStatus,
      ntType: this.product.ntType,
      ntUser: this.product.ntUser,
      orderNo: this.product.orderNo,
  }
    // 调用接口
    this.noticeService.editPro(fxjLoanNotice).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if (data.ok === true) {
        this.msg.success('保存成功');

        // 保存notice表
        this.noticeService.editProA(fxjLoanNoticea).pipe(
          catchError(err => {
            return of(err);
          })
        ).subscribe(data => {
          if (data.ok === true) {
            // this.msg.success('保存成功');
            this.emit.sendMessage("RELOAD_GOODS_LIST");
            this.back();
          } else {
            this.msg.error(data['message']);
          }
        });

        // this.emit.sendMessage("RELOAD_GOODS_LIST");
        // this.back();
      } else {
        this.msg.error(data['message']);
      }
    });
  }

  saveemen() {
    if(this.emen.emergencyMobile==""||this.emen.emergencyMobile==null){
      this.msg.warning("电话");
      return;
    }
    let emen = {

      emergencyContact: this.emen.emergencyContact,
      emergencyMobile:this.emen.emergencyMobile,
      emergencyOffice: this.emen.emergencyOffice,
      orderNo:this.product.orderNo

    };
    this.loading = true;
    this.noticeService.savemen(emen).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.loading = false;
      if (data.ok === true) {
        // this.stemen.reload();
        this.msg.success("成功");
        this.emit.sendMessage("RELOAD_GOODS_LIST");
      } else {
        this.msg.error(data.respMsg);
      }
    })
  }

  changeColor(res) {
    this.product.colorSort = res.color.hex;
  }

  handlePreview = (file: UploadFile) => {
    console.log("sssssssssssssssssssssssssssss");
    console.log(file);
    this.previewImage = file.url || file.thumbUrl;
    this.previewVisible = true;
  }
  
  // 图片 wuwq
  upLoadChange(event) {
    console.log(event);
    let file = event ? event.file : null;
    console.log(file);
    let datas = file && file.id ? file : file.response;
    console.log(datas);
    if (datas) {
        if (event.type == 'success') {
            console.log('ss');
        } else if (event.type == 'removed') {
          console.log('aa');
          let picId = file.id ? file.id : file.response[0].fileId
          this.delPic(picId);
        }
    }
  }


  // 删除图片 wuwq
  delPic(id) {
    this.noticeService.delfile(id).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        if(data.ok){
          // this.productorder = data.data;
          // console.log(this.productorder);
          // this.reload();
        }else{
          console.log("22")
        }
      })
  }

}